package com.flot.yazb.home.setting;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.flot.yazb.R;
import com.flot.yazb.base.BActivity;
import com.flot.yazb.base.BApplication;
import com.flot.yazb.base.adapter.recycleview.BRAdapter;
import com.flot.yazb.base.adapter.recycleview.RVAdapter;
import com.flot.yazb.base.adapter.recycleview.RvHolder;
import com.flot.yazb.base.model.BModel;
import com.flot.yazb.aop.stub.CodeStub;
import com.flot.yazb.model.CityModel;
import com.flot.yazb.model.fate.FateModel;
import com.flot.yazb.model.nearly.TranslatingModel;
import com.flot.yazb.model.user.SelectFriendModel;
import com.flot.yazb.model.user.UserModel;
import com.flot.yazb.model.user.ZhuboDetailModel;
import com.flot.yazb.home.moudle.message.ChatActivity;
import com.flot.yazb.widget.utils.FileAssertUtils;
import com.flot.yazb.widget.utils.GlideImageLoaderUtil;
import com.flot.yazb.widget.utils.ToastUtil;
import com.flot.yazb.widget.MediaPreviewActivity;
import com.flot.yazb.widget.PayDialog;
import com.google.gson.Gson;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;



public class UserDetailActivity extends BActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.iv_icon)
    ImageView ivIcon;
    @BindView(R.id.yearAndaddressAndHeight)
    TextView yearAndaddressAndHeight;
    @BindView(R.id.checkPhone)
    TextView checkPhone;
    @BindView(R.id.checkSfz)
    TextView checkSfz;
    @BindView(R.id.checkWxchat)
    TextView checkWxchat;
    @BindView(R.id.picRecycleView)
    RecyclerView picRecycleView;
    @BindView(R.id.tvmobile)
    TextView tvmobile;
    @BindView(R.id.tvqq)
    TextView tvqq;
    @BindView(R.id.tvwx)
    TextView tvwx;
    @BindView(R.id.tvbeizhu)
    TextView tvbeizhu;
    @BindView(R.id.tvGradu)
    TextView tvGradu;
    @BindView(R.id.tvMoney)
    TextView tvMoney;
    @BindView(R.id.tvWork)
    TextView tvWork;
    @BindView(R.id.tvWeight)
    TextView tvWeight;
    @BindView(R.id.tvHeight)
    TextView tvHeight;
    @BindView(R.id.tvXuexing)
    TextView tvXuexing;
    @BindView(R.id.tvXHunyin)
    TextView tvXHunyin;
    @BindView(R.id.tvRoom)
    TextView tvRoom;
    @BindView(R.id.tvChild)
    TextView tvChild;
    @BindView(R.id.tvyidi)
    TextView tvyidi;
    @BindView(R.id.tvyiditype)
    TextView tvyiditype;
    @BindView(R.id.tvSexlove)
    TextView tvSexlove;
    @BindView(R.id.tvTogetherwithmm)
    TextView tvTogetherwithmm;
    @BindView(R.id.tvmeili)
    TextView tvmeili;
    @BindView(R.id.tvlove)
    TextView tvlove;
    @BindView(R.id.tvtezheng)
    TextView tvtezheng;
    @BindView(R.id.tvoften)
    TextView tvoften;
    @BindView(R.id.tvAges)
    TextView tvAges;
    @BindView(R.id.tvHeightdistance)
    TextView tvHeightdistance;
    @BindView(R.id.tvLowestEdu)
    TextView tvLowestEdu;
    @BindView(R.id.tvLowestMoney)
    TextView tvLowestMoney;
    @BindView(R.id.tv_pritalk)
    TextView tvPritalk;
    @BindView(R.id.tvsendliwu)
    TextView tvsendliwu;
    @BindView(R.id.tvnowtalk)
    TextView tvnowtalk;

    private String[] ageArry = new String[]{"不限", "18-25", "26-35", "36-45", "46-55", "55以上"};
    private String[] heightArray = new String[]{"不限", "160以下", "161-165", "166-170", "170以上"};
    private String[] eduArray = new String[]{"不限", "高中,中专及以上", "大专及以上", "本科及以上"};
    private String[] incomeArray = new String[]{"不限", "2000以上", "5000以上", "10000以上"};
    private List<ZhuboDetailModel.ImgsModel> imgList = new ArrayList();//相册集合
    private FateModel agrModel;
    private UserModel meModel = BApplication.getInstance().getUserModel();
    public ZhuboDetailModel ZModel;
    private BRAdapter<ZhuboDetailModel.ImgsModel> imgAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhubo_page);
        ButterKnife.bind(this);
        agrModel = (FateModel) getIntent().getSerializableExtra("mid");
        tvName.setText(agrModel.name);
        name.setText(agrModel.name);
        initImgAdapter();
        getUserDetail();
        getMsg();
    }


    private void initImgAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        picRecycleView.setLayoutManager(linearLayoutManager);
        imgAdapter = new BRAdapter<ZhuboDetailModel.ImgsModel>(this, R.layout.activity_img_item, imgList) {
            @Override
            protected void convert(RvHolder holder, ZhuboDetailModel.ImgsModel imgsModel, int position) {
                GlideImageLoaderUtil.loadImage((ImageView) holder.getView(R.id.pic), imgsModel.thumb);
            }
        };

        imgAdapter.setOnItemClickListener(new RVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Log.d("setOnItemClickListener", "onItemClick: " + position);
                Intent intent = new Intent(UserDetailActivity.this, MediaPreviewActivity.class);
                intent.putExtra("index", position);
                intent.putExtra("data", (Serializable) imgList);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View v, int position) {

            }
        });
        picRecycleView.setAdapter(imgAdapter);
    }


    private void getUserDetail() {
        showProgress();
        if (agrModel == null) {
            dismissProgress();
            return;
        }
        ZhuboDetailModel.getUserDetail(agrModel.mid, new BModel.Transit<ZhuboDetailModel>(this) {

            @Override
            public void onBorn(ZhuboDetailModel serializable, String code, String message) {
                dismissProgress();
                if (code.equals(CodeStub.B_OK)) {
                    ZModel = serializable;
                    GlideImageLoaderUtil.loadImage(ivIcon, agrModel.thumb);
                    yearAndaddressAndHeight.setText(agrModel.age + " · " + serializable._$0.province + " · " + serializable._$0.city + " · " + serializable._$0.height + "cm");
                    imgList.clear();
                    imgList.addAll(serializable.imgs);
                    imgAdapter.notifyDataSetChanged();

                    tvmobile.setText(serializable._$0.mobile);
                    tvqq.setText(serializable._$0.qq);
                    tvwx.setText(serializable._$0.wechat);
                    tvbeizhu.setText(serializable._$0.beizhu);

                    tvGradu.setText(serializable._$0.education);
                    tvMoney.setText(serializable._$0.income);
                    tvWork.setText(serializable._$0.job);
                    tvWeight.setText(serializable._$0.weight);
                    tvHeight.setText(serializable._$0.height + "cm");
                    tvXuexing.setText(serializable._$0.blood);
                    tvXHunyin.setText(serializable._$0.marriage);
                    tvRoom.setText(serializable._$0.house);

                    tvChild.setText(serializable._$0.child);
                    tvyidi.setText(serializable._$0.yidi);
                    tvyiditype.setText(serializable._$0.leixin);
                    tvSexlove.setText(serializable._$0.sex);
                    tvTogetherwithmm.setText(serializable._$0.fumu);
                    tvmeili.setText(serializable._$0.meili);
                    tvlove.setText(serializable._$0.aihao);
                    tvtezheng.setText(serializable._$0.tezheng);

                } else {
                    ToastUtil.showToast(activity, message);
                }
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.checkPhone, R.id.checkSfz, R.id.checkWxchat, R.id.tvseemobile, R.id.tvseeqq, R.id.tvseewx,
            R.id.tv_pritalk, R.id.tvsendliwu, R.id.tvnowtalk, R.id.tvcall})
    public void onClick(View view) {
        long curtime = System.currentTimeMillis() / 1000;
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tvseemobile:
                if (curtime > meModel.vip) {
                    PayDialog dialog = new PayDialog(this, "VIP用户可查看对方手机号");
                    dialog.show();
                } else {
                    if (ZModel != null)
                        getWXP(0);
                }
                break;
            case R.id.tvseeqq:
                if (curtime > meModel.vip) {
                    PayDialog dialog = new PayDialog(this, "VIP用户可查看对方QQ");
                    dialog.show();
                } else {
                    if (ZModel != null)
                        getWXP(1);
                }
                break;
            case R.id.tvseewx:
                if (curtime > meModel.vip) {
                    PayDialog dialog = new PayDialog(this, "VIP用户可查看对方微信号");
                    dialog.show();
                } else {
                    if (ZModel != null)
                        getWXP(2);
                }
                break;
            case R.id.tv_pritalk:
                if (curtime > meModel.vip) {
                    PayDialog dialog = new PayDialog(this, "权限不足，您需要开通VIP才能\n和美女视频通话");
                    dialog.show();
                } else {
                    if (ZModel != null)
                        ToastUtil.showToast(activity, "预约主播成功！");
                }
                break;
            case R.id.tvsendliwu:
                if (curtime > meModel.vip) {
                    PayDialog dialog = new PayDialog(this, "余额不足，您需要充值\n才能送礼物");
                    dialog.show();
                } else {
                    if (ZModel != null)
                        ToastUtil.showToast(activity, "主播上线之后即可赠送礼物！");
                }
                break;
            case R.id.tvnowtalk:
                if (curtime > meModel.vip) {
                    PayDialog dialog = new PayDialog(this, "权限不足，您需要开通VIP才能\n和美女视频通话");
                    dialog.show();
                } else {
                    if (ZModel != null) {
                        Intent intrne = new Intent(this, ChatActivity.class);
                        intrne.putExtra("name", ZModel._$0.name);
                        intrne.putExtra("mid", ZModel._$0.mid);
                        startActivity(intrne);
                    }

                }
                break;

            case R.id.tvcall:
                TranslatingModel.transPeople(ZModel._$0.mid, new BModel.Transit<TranslatingModel[]>(this) {
                    @Override
                    public void onBorn(TranslatingModel[] serializable, String code, String message) {
                        if (code.equals(CodeStub.B_OK)) {
                            ToastUtil.showToast(UserDetailActivity.this, "您打招呼成功，正在等TA的回复！");
                        } else {
                            ToastUtil.showToast(UserDetailActivity.this, message);
                        }
                    }
                });
                break;
        }
    }
    private ZhuboDetailModel._$0Model wxpModel;

    private void getWXP(final int type) {
        if (wxpModel != null) {
            showwxp(type);
        } else {
            ZhuboDetailModel.getUserWXP(ZModel._$0.mid, new BModel.Transit<ZhuboDetailModel._$0Model>(this) {
                @Override
                public void onBorn(ZhuboDetailModel._$0Model serializable, String code, String message) {
                    if (code.equals(CodeStub.B_OK) && serializable != null) {
                        wxpModel = serializable;
                        showwxp(type);
                    }
                }
            });
        }
    }

    public void showwxp(int type) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(UserDetailActivity.this);
        if (type == 0) {
            dialog.setMessage(wxpModel.phone)
                    .setTitle("手机号");
        } else if (type == 1) {
            dialog.setMessage(wxpModel.qq)
                    .setTitle("QQ号");
        } else {
            dialog.setMessage(wxpModel.weixin)
                    .setTitle("微信号");
        }
        dialog.show();
    }

    /**
     * 获取征友条件信息
     */
    private void getMsg() {
        showProgress();
        String json = FileAssertUtils.readAssets(this, "province.json");
        Gson gson = new Gson();
        final CityModel cityModel = gson.fromJson(json, CityModel.class);
        SelectFriendModel.getFriendModel(meModel.id, new BModel.Transit<SelectFriendModel>(this) {
            @Override
            public void onBorn(SelectFriendModel serializable, String code, String message) {
                dismissProgress();
                if (code.equals(CodeStub.B_OK)) {
                    if (serializable != null) {
                        if (serializable.age != null || "".equals(serializable.age)) {
                            tvAges.setText(ageArry[(Integer.parseInt(serializable.age))]);
                        }

                        if (serializable.height != null || "".equals(serializable.height)) {
                            tvHeightdistance.setText(heightArray[(Integer.parseInt(serializable.height))] + "cm");
                        }

                        if (serializable.education != null || "".equals(serializable.education)) {
                            tvLowestEdu.setText(eduArray[(Integer.parseInt(serializable.education))]);
                        }

                        if (serializable.income != null || "".equals(serializable.income)) {
                            tvLowestMoney.setText(incomeArray[(Integer.parseInt(serializable.income))]);
                        }

                        if (serializable.province != null || "".equals(serializable.province)) {
                            for (int i = 0; i < cityModel.getIdList().size(); i++) {
                                if (cityModel.getIdList().get(i).equals(serializable.province)) {
                                    tvoften.setText(cityModel.getStringList().get(i));
                                }
                            }
                        }

                    }
                } else {
                    ToastUtil.showToast(activity, message);
                }
            }
        });
    }
}
